﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class Int64Processor : GenericDataProcessor<long>
        {
            public override bool IsSystem
            {
                get
                {
                    return true;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "long";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "long",
                    "int64",
                    "system.int64"
                };
            }

            public override long Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return long.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                binaryWriter.Write7BitEncodedInt64(Parse(value));
            }
        }
        private sealed class Int64ArrayProcessor : GenericDataArrayProcessor<long>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "long[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "long[]",
                    "int64[]",
                    "system.int64[]"
                };
            }

            public override long ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return long.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class Int64ListProcessor : GenericDataListProcessor<long>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<long>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<long>",
                    "list<int64>",
                    "list<system.int64>",
                    "system.collections.generic.list<long>",
                    "system.collections.generic.list<int64>",
                    "system.collections.generic.list<system.int64>",
                };
            }

            public override long ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return long.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class Int64DictionaryProcessor : GenericDataDictionaryProcessor<long>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,long>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,long>",
                    "dictionary<string,int64>",
                    "dictionary<string,system.int64>",
                    "dictionary<system.string,long>",
                    "dictionary<system.string,int64>",
                    "dictionary<system.string,system.int64>",
                    "system.collections.generic.dictionary<string,long>",
                    "system.collections.generic.dictionary<string,int64>",
                    "system.collections.generic.dictionary<string,system.int64",
                    "system.collections.generic.dictionary<system.string,long>",
                    "system.collections.generic.dictionary<system.string,int64>",
                    "system.collections.generic.dictionary<system.string,system.int64>",
                };
            }

            public override long ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return long.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    binaryWriter.Write(item.Value);
                }
            }
        }
    }
}
